package de.fhkl.android.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MainService extends Service {
	private String tag = "service";
	public static Context context;
	private Thread thread;
	public IBinder onBind(Intent arg0) {
		Log.i(tag, "start IBinder!");
		return null;
	}

	public void onCreate() {
		Log.i(tag, "start onCreate!");
		super.onCreate();
		this.context = this.getApplicationContext();
		//R.string.mensa;
		thread=new Thread(){
			public void run(){
				Log.i(tag,"Thread starts!");
				while(true){
					try {
						Log.i(tag,"MainService is alive!");
						this.sleep(3000);
					} catch (InterruptedException e) {
						Log.e(tag,e.getMessage());
					}
				}	
			}
		};
		
	}

	@Override
	public void onStart(Intent intent, int startId) {
		if (thread.isAlive()){
			Log.w(tag, "Service already start");
		} else {
			Log.w(tag, "Thread Start!");
			thread.start();
		}
		
		super.onStart(intent, startId);
	}

	@Override
	public void onDestroy() {
		Log.i(tag, "start onDestroy!");
		thread.stop();
		thread.destroy();
		super.onDestroy();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.i(tag, "start onUnbind!");
		return super.onUnbind(intent);
	}

}
